/*
 * Copyright 2020-2030 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.framework.domain.dto;

import java.io.PrintWriter;
import java.io.StringWriter;
import com.alphalab.framework.domain.IValueObject;
import com.alphalab.framework.exception.MoRuntimeException;
import com.alphalab.framework.types.CommonResultCodeEnum;

/**
 * 结果对象基类.
 * @author allen
 * @version 1.0.0 2020年3月2日
 */
public class ResultVO implements IValueObject {

	/**
	 * 返回码.
	 */
	protected String code;
	
	/**
	 * 返回描述.
	 */
	protected String msg;
	
	/**
	 * 错误详情/异常堆栈信息.
	 */
	protected String errorDetail;
	
	/**
	 * 是否执行成功.
	 */
	protected boolean success;
	
	public ResultVO() {
	}
	
	/**
	 * 构造异常情况下结果对象.
	 * @param throwable Throwable异常
	 */
	public ResultVO(Throwable throwable) {
		if (throwable instanceof MoRuntimeException) {
			MoRuntimeException moex = (MoRuntimeException)throwable;
			this.code = moex.getErrorCode();
			this.msg = moex.getMessage();
		} else {
			this.code = CommonResultCodeEnum.ERROR_HANDLE.getCode();
	    	this.msg = CommonResultCodeEnum.ERROR_HANDLE.getMsg();
		}
    	this.errorDetail = getStackTrace(throwable);
    	this.success = false;
	}
	
	/**
	 * 构造异常情况下结果对象.
	 * @param errorCode 错误码
	 * @param errorMsg 错误信息
	 */
	public ResultVO(String errorCode, String errorMsg) {
		this.code = errorCode;
    	this.msg = errorMsg;
    	this.success = false;		
	}
	
	/**
	 * 构造异常情况下结果对象.
	 * @param errorCode 错误码
	 * @param errorMsg 错误信息
	 * @param throwable Throwable异常
	 */
	public ResultVO(String errorCode, String errorMsg, Throwable throwable) {
		this(errorCode, errorMsg);
		this.errorDetail = getStackTrace(throwable);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean getSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	public String getErrorDetail() {
		return errorDetail;
	}

	public void setErrorDetail(String errorDetail) {
		this.errorDetail = errorDetail;
	}
	
	/**
	 * 获取字符串形式的异常堆栈信息.
	 * @param throwable Throwable异常
	 * @return String
	 */
	private static String getStackTrace(Throwable throwable) {
	    StringWriter stringWriter= new StringWriter();
	    PrintWriter writer= new PrintWriter(stringWriter);
	    throwable.printStackTrace(writer);
	    StringBuffer buffer= stringWriter.getBuffer();
	    return buffer.toString();
	}
	
	/**
	 * 设置处理成功结果.
	 */
	public void setSuccessResult() {
		this.code = CommonResultCodeEnum.SUCCESS_HANDLE.getCode();
    	this.msg =CommonResultCodeEnum.SUCCESS_HANDLE.getMsg();
    	this.success = true;
    }

	@Override
	public String toString() {
		return "ResultVO [code=" + code + ", msg=" + msg + ", success="
				+ success + "]";
	}
	
}
